package com.cch.cooperation.api.zone.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cch.cooperation.common.util.SnowflakeIdUtil;
import com.cch.cooperation.dao.zone.enums.ZoneRemindItemEnum;
import com.cch.cooperation.dao.zone.mapper.ZoneRemindItemMapper;
import com.cch.cooperation.dao.zone.model.ZoneRemindItem;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 提醒事项明细
 *
 * @author cch
 */
@Service
public class ZoneRemindItemServiceImpl {
    
    @Resource
    private ZoneRemindItemMapper mapper;

    public Boolean isFirstComplete(String zoneRemindId, Date remindTime) {
        LambdaQueryWrapper<ZoneRemindItem> query = new LambdaQueryWrapper<>();
        query.eq(ZoneRemindItem::getZoneRemindId, zoneRemindId);
        if (ObjectUtil.isNotNull(remindTime)) {
            query.eq(ZoneRemindItem::getRemindTime, remindTime);
        }
        query.eq(ZoneRemindItem::getStatus, ZoneRemindItemEnum.Status.COMPLETED.value);
        return mapper.selectCount(query) == 1;
    }

    public List<ZoneRemindItem> listByZoneRemindIds(List<String> zoneRemindIds) {
        if (CollUtil.isEmpty(zoneRemindIds)) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<ZoneRemindItem> query = new LambdaQueryWrapper<>();
        query.in(ZoneRemindItem::getZoneRemindId, zoneRemindIds);
        return mapper.selectList(query);
    }

    public List<ZoneRemindItem> listByUserId(String zoneId, String userId) {
        return mapper.listByUserId(zoneId, userId);
    }

    public void add(ZoneRemindItem req) {
        if (StrUtil.isEmpty(req.getId())) {
            req.setId(SnowflakeIdUtil.getIdStr());
        }
        req.setCreateTime(new Date());
        req.setStatus(ZoneRemindItemEnum.Status.UN_COMPLETE.value);
        mapper.insert(req);
    }

    public void updateById(ZoneRemindItem req) {
        mapper.updateById(req);
    }

    public ZoneRemindItem getById(String id) {
        return mapper.selectById(id);
    }

    public void deleteById(String id) {
        mapper.deleteById(id);
    }

    public void deleteByRemindId(String remindId, Integer status) {
        LambdaQueryWrapper<ZoneRemindItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZoneRemindItem::getZoneRemindId, remindId);
        queryWrapper.eq(Objects.nonNull(status), ZoneRemindItem::getStatus, status);
        mapper.delete(new LambdaQueryWrapper<ZoneRemindItem>().eq(ZoneRemindItem::getZoneRemindId, remindId));
    }

    public List<ZoneRemindItem> queryWaitNotices() {
        LambdaQueryWrapper<ZoneRemindItem> query = new LambdaQueryWrapper<>();
        query.eq(ZoneRemindItem::getHasNotice, false);
        query.eq(ZoneRemindItem::getStatus, ZoneRemindItemEnum.Status.UN_COMPLETE.value);
        query.isNotNull(ZoneRemindItem::getRemindTime);
        query.ge(ZoneRemindItem::getRemindTime, DateUtil.offsetDay(new Date(), -1));
        return mapper.selectList(query);
    }
}
